#include "../quickjs-libc.h"

int main(int argc, char **argv) {
  JSRuntime *rt;
  JSContext *ctx;
  rt = JS_NewRuntime();
  ctx = JS_NewContextRaw(rt);
  JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
  /* 初始化Object、Function、Number、eval等全局构造函数和方法、以及对应的原型链方法 */
  JS_AddIntrinsicBaseObjects(ctx);
  JS_AddIntrinsicDate(ctx);
  JS_AddIntrinsicEval(ctx);
  JS_AddIntrinsicStringNormalize(ctx);
  JS_AddIntrinsicRegExp(ctx);
  JS_AddIntrinsicJSON(ctx);
  JS_AddIntrinsicProxy(ctx);
  JS_AddIntrinsicMapSet(ctx);
  JS_AddIntrinsicTypedArrays(ctx);
  JS_AddIntrinsicPromise(ctx);
  JS_AddIntrinsicBigInt(ctx);
  /* console.log、print等方法 */
  js_std_add_helpers(ctx, argc, argv);
  
  const char* filename = "./closure.js";
  size_t buf_len;
  uint8_t *buf = js_load_file(ctx, &buf_len, filename);
  JS_Eval(ctx, (char*)buf, buf_len, filename, JS_EVAL_TYPE_GLOBAL);
  
  js_std_loop(ctx);
  JS_FreeContext(ctx);
  JS_FreeRuntime(rt);
  return 0;
}